package com.vehiclemanagement.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.vehiclemanagement.entity.ETCCardRecharge;
import com.vehiclemanagement.service.ETCCardRechargeService;
import com.vehiclemanagement.util.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.List;

/**
 * ETC卡充值记录控制器
 */
@RestController
@RequestMapping("/etc-card-recharge")
public class ETCCardRechargeController {
    
    @Autowired
    private ETCCardRechargeService etcCardRechargeService;
    
    /**
     * 分页查询充值记录列表
     */
    @GetMapping("/list")
    public R<IPage<ETCCardRecharge>> list(
            @RequestParam(value = "current", defaultValue = "1") Integer current,
            @RequestParam(value = "size", defaultValue = "10") Integer size,
            @RequestParam(value = "searchTerm", required = false) String searchTerm,
            @RequestParam(value = "etcCardId", required = false) Long etcCardId,
            @RequestParam(value = "status", required = false) Integer status,
            @RequestParam(value = "startTime", required = false) 
                @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") LocalDateTime startTime,
            @RequestParam(value = "endTime", required = false) 
                @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") LocalDateTime endTime) {
        IPage<ETCCardRecharge> page = etcCardRechargeService.pageRecharges(
                current, size, searchTerm, etcCardId, status, startTime, endTime);
        return R.ok(page);
    }
    
    /**
     * 获取充值记录详情
     */
    @GetMapping("/{id}")
    public R<ETCCardRecharge> getById(@PathVariable Long id) {
        ETCCardRecharge recharge = etcCardRechargeService.getRechargeById(id);
        if (recharge == null) {
            return R.error("充值记录不存在");
        }
        return R.ok(recharge);
    }
    
    /**
     * 添加充值记录
     */
    @PostMapping
    public R<Boolean> add(@RequestBody ETCCardRecharge recharge) {
        System.out.println("接收到添加ETC卡充值记录请求，数据：" + recharge);
        
        // 检查必要字段
        if (recharge.getEtcCardId() == null) {
            return R.error("ETC卡ID不能为空");
        }
        if (recharge.getAmount() == null || recharge.getAmount().compareTo(BigDecimal.ZERO) <= 0) {
            return R.error("充值金额必须大于0");
        }
        
        boolean result = etcCardRechargeService.addRecharge(recharge);
        if (result) {
            return R.ok(true);
        } else {
            System.out.println("添加ETC卡充值记录失败，服务层返回false");
            return R.error("添加失败");
        }
    }
    
    /**
     * 更新充值记录
     */
    @PutMapping
    public R<Boolean> update(@RequestBody ETCCardRecharge recharge) {
        if (recharge.getId() == null) {
            return R.error("充值记录ID不能为空");
        }
        boolean result = etcCardRechargeService.updateRecharge(recharge);
        return result ? R.ok(true) : R.error("更新失败");
    }
    
    /**
     * 删除充值记录
     */
    @DeleteMapping("/{id}")
    public R<Boolean> delete(@PathVariable Long id) {
        System.out.println("接收到删除ETC卡充值记录请求，ID: " + id);
        
        boolean result = etcCardRechargeService.deleteRecharge(id);
        
        if (result) {
            System.out.println("成功删除ETC卡充值记录，ID: " + id);
            return R.ok(true);
        } else {
            System.out.println("删除ETC卡充值记录失败，ID: " + id);
            return R.error("删除失败");
        }
    }
    
    /**
     * 获取ETC卡总充值金额
     */
    @GetMapping("/total-amount/{etcCardId}")
    public R<BigDecimal> getTotalAmount(@PathVariable Long etcCardId) {
        BigDecimal totalAmount = etcCardRechargeService.getTotalRechargeAmount(etcCardId);
        return R.ok(totalAmount);
    }
    
    /**
     * 获取某张ETC卡的充值记录
     */
    @GetMapping("/by-card/{etcCardId}")
    public R<List<ETCCardRecharge>> getByETCCardId(@PathVariable Long etcCardId) {
        List<ETCCardRecharge> recharges = etcCardRechargeService.getRechargesByETCCardId(etcCardId);
        return R.ok(recharges);
    }
    
    /**
     * 测试接口
     */
    @GetMapping("/test")
    public R<String> test() {
        return R.ok("ETC卡充值记录API正常运行！");
    }
} 